﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

namespace BaLs.Models
{
    public class BaLsRespository:IBaLsRespository
    {
        private BalsDataContext db = new BalsDataContext();

        public IQueryable<Link> GetAllLink()
        { return db.Links; }

        public IQueryable<Link> GetAllPublicLink()
        {
            return from link in GetAllLink()
                   where link.private_flag.Value=='F'
                   orderby link.create_date descending
                   select link;
        }


        public IQueryable<Link> GetLinkByUser(string userid)
        {
            return from link in GetAllLink()
                   where link.owner.Equals(userid) orderby link.create_date descending
                   select link;        
        }

        public IQueryable<Link> GetLinkByTags(EntitySet<Tag> searchTags)
        {
            var tags = from tag in db.Tags
                       where tag.tag_desc.Equals(searchTags[0].tag_desc)
                       select tag;

            


            EntitySet<Link> searchedLinks=new EntitySet<Link>();
            
            foreach (Tag tag in getTags(searchTags))
            {
                var plinks = from link in GetAllPublicLink()
                             where link.Tags.Contains(tag)
                             orderby link.create_date descending
                             select link;

                searchedLinks.AddRange(plinks);
                
            }

            return searchedLinks.AsQueryable<Link>();
        }
        public IQueryable<Link> GetPublicLinkByUser(string userid)
        {
            return from link in GetLinkByUser(userid)
                   where link.private_flag=='F'           
                   orderby link.create_date descending
                   select link;
        }

        public Link GetLink(long id)
        {
            return db.Links.SingleOrDefault(l => l.link_id == id);
        }

        public void Delete(Link link)
        {
            db.Tags.DeleteAllOnSubmit(link.Tags);
            db.Links.DeleteOnSubmit(link);        
        }

        public void Add(Link link)
        { db.Links.InsertOnSubmit(link); }

        public void Add(user user)
        { db.users.InsertOnSubmit(user); }

        public void Save()
        {
            db.SubmitChanges();
        }

        public IQueryable<user> GetAllUser()
        { return db.users; }

        public user GetUser(String userId)
        {
            return db.users.SingleOrDefault(user => user.userid.Equals(userId));
        }

        public user GetUserByEmail(String email)
        {
            return db.users.SingleOrDefault(user => user.email.Equals(email));
        }

        public bool isLinkExist(String url, String userid)
         {
             Link linkToCheck = GetLinkByUser(userid).SingleOrDefault(link => link.url.Equals(url));

             if (linkToCheck == null)
                 return false;
             else
                 return true;
         }

        private EntitySet<Tag> getTags(EntitySet<Tag> tagSet)
        {
            EntitySet<Tag> newTagSet = new EntitySet<Tag>();

            foreach (Tag t in tagSet)
            {
                var tags = from tag in db.Tags
                           where tag.tag_desc.Equals(t.tag_desc)
                           select tag;

                newTagSet.AddRange(tags);
            }


            return newTagSet;
        }

    }
}
